﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace FirstGame
{
    public class CharacterManager : GameObjectManager
    {
        private String fileXml;
        XmlDocument doc;
        public CharacterManager(String fileXML)
        {
            fileXml = fileXML;

            try
            {
                doc = new XmlDocument();
                doc.Load(fileXML);
                nObject = doc.SelectNodes(@"//Character").Count;
                GameObject  = new VisibleGameEntity[nObject];
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public override bool Init(ContentManager content)
        {
            XmlNode node = doc.SelectNodes(@"//Character")[0];
            GameObject[0] = new Player();
            ((Player) GameObject[0]).NSprite = 56;
            ((Player)GameObject[0]).Sprites=new List<My2Dsprite>();

            XmlNode nodestand = doc.SelectSingleNode(@"Stand");
            List<My2Dsprite> Stand = LoadSprites(nodestand, content);

            XmlNode nodemove = doc.SelectSingleNode(@"Move");
            List<My2Dsprite> Move = LoadSprites(nodemove, content);

            XmlNode nodeattract1 = doc.SelectSingleNode(@"Attract1");
            List<My2Dsprite> Attract1 = LoadSprites(nodeattract1, content);

            XmlNode nodeattract2 = doc.SelectSingleNode(@"Attract2");
            List<My2Dsprite> Attract2 = LoadSprites(nodeattract2, content);

            XmlNode nodedying = doc.SelectSingleNode(@"Dying");
            List<My2Dsprite> Dying = LoadSprites(nodedying, content);

            XmlNode nodedyed = doc.SelectSingleNode(@"Dyed");
            List<My2Dsprite> Dyed = LoadSprites(nodedyed, content);

            XmlNode nodeskill= doc.SelectSingleNode(@"Skill");
            List<My2Dsprite> Skill = LoadSprites(nodeskill, content);


            return false;

        }
        public List<My2Dsprite> LoadSprites(XmlNode node, ContentManager content)
        {
            List<My2Dsprite> tempListSprite = new List<My2Dsprite>();
            int nTexture = int.Parse(node.SelectSingleNode(@"NumOfFrame").InnerText);
            String pathImage = node.SelectSingleNode("ContentName").InnerText;
            My2Dsprite temp2DSprite;
            for(int i =0;i<8;i++)
            {
                Texture2D[] temp= new Texture2D[nTexture];
                for (int j = 0; j < nTexture; j++)
                {
                    temp[j] = content.Load<Texture2D>(pathImage + i.ToString("00") + "-" + j.ToString("00"));
                }
                temp2DSprite = new My2Dsprite(temp,new Vector2(0,0));
                tempListSprite.Add(temp2DSprite);
            }
            return tempListSprite;
        }
    }
}
